//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
// const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
    data: {
        hei2:500,
        list:[],
        active:true,
        total:0,
        page:1,
        
    },
    onShow(){
        this.setData({
            
             hei2:app.globalData.screenHeight-150-130,
             
        })
        this.requstList('110902','140302','1');
    },
    backHome:function(){
        wx.switchTab({
            url:'../index/index'
        })
    },
    backMy:function(){
        wx.navigateBack();
    },
    clickLeft(){
        this.setData({
            active:true,
            page:1
        })
        this.requstList('110902','140302','1');
    },
    clickRight(){
        this.setData({
            active:false,
            page:1
        })
    this.requstList('110903','140303','1');
    },
    
    requstList: function(payState,orderState,page) {
    //     var i = 0;
         if (page == 1) {
             this.setData({
                 list:[],
                 total:0
             })
    }
        let that = this;
    
        var data = {
            "asc": true,
            "isChose": true,
            "limit": 10,
            "orderState": orderState,
            "payState":payState,
            "page": page,
            "sidx": ""
        }
    
        Api.orderListApi(data).then(res => {
            console.log(res);
            if (res.code == 0) {
                var arr = res.data.records;
                
            
                if (arr.length == 0) {
                    return;
                }
            
                var oldArr = that.data.list;
            
                var newArr = oldArr.concat(arr);
            
                that.setData({
                   list: newArr,
                   total:res.data.total,
                    isShow: false,
                })
            } else if (res.code == "401") {
                that.setData({
                    isShow: true,
                })
            
            } else {
            
            }
            
        })
           
    
   
       
    },
    clickDe: function(e) {
    
        let i = e.currentTarget.dataset.inde;

        var comSta = `list[${i}].isSelect`;
        let a = this.data.list[i].isSelect;
        if (a == true) {
            this.setData({
                [comSta]: false
            })
        } else {
            this.setData({
                [comSta]: true
            })
        }
        },
    
    refreshBottom: function(e) {
        // var i = e.currentTarget.dataset.index;
        // let list = `tabsArray[${i}].list`;
    
        // var type = "" + e.currentTarget.dataset.index;
        // var ty = "";
    if(this.data.active){
        
        var a = this.data.page;
        var total = this.data.total;
        var list = this.data.list;
        
        if (list.length < total) {
            a++;
            this.setData({
                page: a
            })
             this.requstList('110902','140302',a);
          }
       
    }else{
        var a = this.data.page;
        var total = this.data.total;
        var list = this.data.list;
        
        if (list.length < total) {
            a++;
            this.setData({
                page: a
            })
             this.requstList('110903','140303',a);
          }
      
        
    }
        
        
    
    
    },
    Sure(){
        let that =this;
        let arr = this.data.list;
        let NewArray = [];
        arr.forEach(item=>{
            if(item.isSelect){
                NewArray.push(item.ckOrderNo);
            }
        })
        if(NewArray.length == 0){
            wx.showToast({
                title:'请至少选择一笔订单进行确认!',
                icon:'none'
            })
        }else{
            
             Api.confirm_payApi(NewArray).then(res=>{
                 if (res.code == 0) {
                     that.wxPay(res.weChatAppletOrder);
                 } else {
                     wx.showToast({
                         title: resp.msg
                     })
                 }
                  // 
                 
             })
        }
       
        
    },
    wxPay: function(weChatAppletOrder) {
    
        var that = this;
        wx.requestPayment({
            timeStamp: weChatAppletOrder.timeStamp,
            nonceStr: weChatAppletOrder.nonceStr,
            package: weChatAppletOrder.pay_package,
            signType: weChatAppletOrder.signType,
            paySign: weChatAppletOrder.paySign,
            success: function(res) {
                that.setData({
                    active:false,
                })
                // wx.showToast({
                //     title: '支付成功',
                //     icon:'none'
                // })
               that.requstList('110903','140303',1);
            }
        })
    },
    
    
      
    Cancel(){
          let that =this;
          let arr = this.data.list;
          let NewArray = [];
            arr.forEach(item=>{
                if(item.isSelect){
                    NewArray.push(item.ckOrderNo);
                }
            })
            if(NewArray.length == 0){
                wx.showLoading({
                    title:'请至少选择一笔订单进行取消!',
                    icon:'none'
                })
            }else{
                
                 Api.confirm_cancelApi(NewArray).then(res=>{
                      that.requstList('110902','140302',1);
                 })
            }                  
    },
    backHome(){
        wx.switchTab({
            url:'../index/index'
        })
    },
    backMy(){
        wx.switchTab({
            url:'../My/index'
        })
    },
    
  
  

  







});
